package com.hbue.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hbue.springboot.pojo.TbWorker;
import com.hbue.springboot.service.ManageWorkerService;
import com.hbue.springboot.service.TbWorkerService;
import com.hbue.springboot.utils.QueryParams;
import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping(value = "/TbWorker")
@RequiredArgsConstructor
public class ManageWorkerController {
    private final ManageWorkerService manageWorkerService;

    @Value("${file.upload.path}")
    private String fileUploadPath;

    @GetMapping("/getAllWorkers")
    public List<TbWorker> getAllWorker() {
        return manageWorkerService.getAllWorkers();
    }

    @PostMapping("/addWorker")
    public int addWorker(@RequestBody TbWorker tbWorker) {
        return manageWorkerService.addWorker(tbWorker);
    }

    @DeleteMapping("/deleteWorker/{id}")
    public int deleteWorker(@PathVariable int id) {
        return manageWorkerService.deleteWorker(id);
    }

    @PutMapping("/updateWorker")
    public int updateWorker(@RequestBody TbWorker tbWorker) {
        return manageWorkerService.updateWorker(tbWorker);
    }

    @GetMapping("/getWorkerByName")
    public List<TbWorker> getWorkerByName(@RequestParam String name) {
        return manageWorkerService.getWorkerByName(name);
    }

    @PutMapping("/updateWorkerSalary")
    public int updateWorkerSalary(@RequestParam("money") int money, @RequestParam("id") int id) {
        return manageWorkerService.updateWorkerSalary(money, id);
    }

    @PostMapping("listWorkersByPage")
    public IPage<TbWorker> listWorkersByPage(@RequestBody QueryParams queryParams) {
        return manageWorkerService.listWorker(queryParams);
    }

    @PostMapping("/saveWorker")
    public Boolean saveOrUpdateWorker(
            TbWorker worker,
            @RequestParam(name = "multipartFile", required = false)
            MultipartFile multipartFile) {
        // 条件成立：表示前端添加送水工并且上传了送水工照片，将照片写入到
        if (null != multipartFile) {
            String fileName = multipartFile.getOriginalFilename();
            File uploadPath = new File(fileUploadPath);
            // 条件成立：表示该路径在磁盘中不存在，将其创建
            if (!uploadPath.exists()) {
                uploadPath.mkdirs();
            }
            int index = fileName.lastIndexOf(".");
            String suffix = fileName.substring(index);
            long prefix = System.nanoTime();
            String fullName = prefix + suffix;
            File uploadFilePath = new File(uploadPath, fullName);
            try {
                // 上传照片到服务器
                multipartFile.transferTo(uploadFilePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            worker.setWorkerImage(fullName);
        }
        boolean flag = manageWorkerService.saveOrUpdate(worker) > 0;
        return flag;
    }

}
